#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import time
# 将csv转换成txt格式，空格分离
from tqdm import tqdm

def parse_comma_to_space(outpath):
    fout = open(outpath, "w")
    with open(file_in, 'r') as fin:
        for line in tqdm(fin):
            line_new = line.replace(",", " ")
            fout.write(line_new)
    fout.close()


if __name__ == "__main__":
    raise ValueError
    st = time.time()
    # file_dir = "../db/dataset/"
    file_dir = "../data/ebs_segment_io_records/AY283G_2022-01-01_00:00:00_2022-01-02_00:00:00/device/"
    out_dir = "../ebs_segment_dataset/"
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)
        print('输出文件夹不存在，新建它')

    # filename_pures = ['277575252013355', '562950162059083', '562950162059084']
    # filename_pures = ['562949963339941', '562949953436138']
    # filename_pures = ['562949965360787']
    # filename_pures = ['562949967559910', '562950001370905', '562949969098071', '562949968347139', '562949969152952', '562949979559507', '562950029174062']

    # filename的选择 if 4.5 < write_number_inLoad and 0.7 < read_pct_inRun:
    filename_pures = ["562949953548589", "562949967749753", "562950186864332", "562949991777769", "562950186864333", "562949965211706",
                      "562949953516488", "562949969104010", "562950127122813", "562949991973235", "562950186864327", "562949953555222",
                      "562949976761450", "562949991973241", "562949991973237", "562950069800157", "562950186864331", "562950186864321",
                      "562949991820923", "562950186864318", "562950010336160", "114499641359965", "562950186864317"]

    if filename_pures == []:
        filenames = os.listdir(file_dir)
    else:
        filenames_all = os.listdir(file_dir)
        filenames = []
        for filename in filenames_all:
            if not filename.split(".csv")[0] in filename_pures:
                filenames.append(filename)

    n = len(filenames)
    i = 0
    for filename in filenames:
        i = i+1
        file_in = file_dir + filename
        file_out = out_dir + filename.split(".")[0] + ".txt"

        if os.path.exists(file_out):
            print("already exist, re generate")
            os.remove(file_out)

        print("generating", file_out, "that is: ", i, "/", n)
        parse_comma_to_space(file_out)
